﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Text;

namespace IHISCETeim.Web.MachineSys.FXModel
{
    public partial class ReportAnalysis : PageBase
    {
        protected override string DWZRef
        {
            get
            {
                return "ReportAnalysis";
            }
        }

        protected int ReportType
        {
            get
            {
                return GetRequestValue<int>("rt", 1);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.AddHeader("pragma", "no-cache");
            Response.CacheControl = "no-cache";


            switch (ReportType)
            {
                case 1:
                    DataBind1();
                    break;
                case 2:
                    DataBind2();
                    break;
                case 3:
                    DataBind3();
                    break;
                case 4:
                    DataBind4();
                    break;
                case 5:
                    DataBind5();
                    break;
                case 6:
                    DataBind6();
                    break;
            }
            //DataBind();
        }

        #region 1经销商放款台数占比分析

        private DataTable _report1dts = null;
        private DataTable report1dts
        {
            get
            {
                if (_report1dts == null)
                {
                    _report1dts = FXFinanceLeaseController.ReportGetJXSFKTS();

                    int oldcolumnNum = _report1dts.Columns.Count;
                    _report1dts.Columns.Add("合计", typeof(int));
                    _report1dts.Columns.Add("占比", typeof(string));

                    int row合计 = 0;
                    int row所有行合计 = 0;//用于占比列
                    foreach (DataRow dr in _report1dts.Rows)
                    {
                        row合计 = 0;
                        for (int cIndex = 1; cIndex < oldcolumnNum; cIndex++)
                        {
                            row合计 += Convert.ToInt32(dr[cIndex]);
                        }
                        dr["合计"] = row合计;
                        row所有行合计 += row合计;
                    }

                    //
                    foreach (DataRow dr in _report1dts.Rows)
                    {
                        row合计 = Convert.ToInt32(dr["合计"]);
                        dr["占比"] = ((Convert.ToDouble(row合计) / row所有行合计) * 100).ToString("f2") + "%";
                       
                    }
                    DataTable dt = _report1dts.Copy();
                    DataView dv = dt.DefaultView;
                    dv.Sort = " 合计 desc";
                   
                    _report1dts = dv.ToTable();
                }
                return _report1dts;
            }
        }
        private void DataBind1()
        {           
            //合计行处理
            DataTable dt = report1dts.Copy();
            DataRow drnew = dt.NewRow();
            //合计
           
            foreach (DataColumn dc in dt.Columns)
            {
                drnew[dc.ColumnName] = 0;
            }
            foreach (DataColumn dc in dt.Columns)
            {
                 if(dc.ColumnName=="经销商" || dc.ColumnName=="占比")
                    continue;
                 foreach (DataRow dr in dt.Rows)
                 {
                     drnew[dc.ColumnName] = Convert.ToInt32(drnew[dc.ColumnName]) + Convert.ToInt32(dr[dc.ColumnName]);
                 }
            }
            drnew["经销商"] = "合计";
            drnew["占比"] = "100%";
            dt.Rows.Add(drnew);

            this.gv1.DataSource = dt;
            this.gv1.DataBind();
            
        }
        List<string> Report1colNames = new List<string>();
        protected string GetReport1JSCategories()
        {
            DataTable dtcl = report1dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int rowindex = 1;
            foreach (DataRow dr in dtcl.Rows)
            {
                string GongWei = dr["经销商"].ToString();
                if (!Report1colNames.Contains(GongWei))
                {
                    sb.AppendFormat("'{0}'", GongWei);
                    Report1colNames.Add(GongWei);
                    if (rowindex != dtcl.Rows.Count)
                    {
                        sb.AppendFormat(",");
                    }
                }

                rowindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport1JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report1dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");

            int rowNum = 1;
            string str直接 = "{name: '合计',data:[";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 += dr["合计"];
                if (rowindex != Report1colNames.Count)
                {
                    str直接 += ",";
                }
                rowindex++;
            }
          
            str直接 += "]}";


            sb.AppendLine(str直接);
            rowNum++;

            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 2融资租赁机型占比分析
        private DataTable _report2dts = null;
        private DataTable report2dts
        {
            get
            {
                if (_report2dts == null)
                {
                    _report2dts = FXFinanceLeaseController.ReportGetRZZRJXZB();

                }
                return _report2dts;
            }
        }
        private void DataBind2()
        {
            //合计行处理
            DataTable dt = report2dts.Copy();
            DataRow drnew = dt.NewRow();
            //合计
           
            foreach (DataColumn dc in dt.Columns)
            {
                drnew[dc.ColumnName] = 0;
            }
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "机型")
                    continue;
                foreach (DataRow dr in dt.Rows)
                {
                    drnew[dc.ColumnName] = Convert.ToInt32(drnew[dc.ColumnName]) + Convert.ToInt32(dr[dc.ColumnName]);
                }
            }
           // drnew["占比"] = "100%";
            drnew["机型"] = "合计";
            dt.Rows.Add(drnew);


            this.gv2.DataSource = dt;
            this.gv2.DataBind();
        }
       
        
        protected string GetReport2JSSeries()
        {
            /*
                [ 'Firefox', 45.0],
				['IE', 26.8],
				{
				    name: 'Chrome',
				    y: 12.8,
				    sliced: true,
				    selected: true
				},
				['Safari', 8.5],
				['Opera', 6.2],
				['Others', 0.7]
             */
            DataTable dtmain = report2dts;
            StringBuilder sb = new StringBuilder();
           // sb.AppendLine("[");

            int rowNum = 1;
            string str直接 = "";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 += string.Format("['{0}', {1}]", dr["机型"], FormatDecimalToStrings(dr["占比"],"G"));
                if (rowindex != report2dts.Rows.Count)
                {
                    str直接 += ",";
                }
                rowindex++;
            }

          //  str直接 += "]}";


            sb.AppendLine(str直接);
            rowNum++;

            //sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 3融资租赁期限占比分析
        private DataTable _report3dts = null;
        private DataTable report3dts
        {
            get
            {
                if (_report3dts == null)
                {
                    _report3dts = FXFinanceLeaseController.ReportGetRZZRQXZB();

                }
                return _report3dts;
            }
        }
        private void DataBind3()
        {
            //合计行处理
            DataTable dt = report3dts.Copy();
            DataRow drnew = dt.NewRow();
            //合计

            foreach (DataColumn dc in dt.Columns)
            {
                drnew[dc.ColumnName] = 0;
            }
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "融资期限")
                    continue;
                foreach (DataRow dr in dt.Rows)
                {
                    drnew[dc.ColumnName] = Convert.ToInt32(drnew[dc.ColumnName]) + Convert.ToInt32(dr[dc.ColumnName]);
                }
            }
            // drnew["占比"] = "100%";
            drnew["融资期限"] = "合计";
            dt.Rows.Add(drnew);



            this.gv3.DataSource = dt;
            this.gv3.DataBind();
        }


        protected string GetReport3JSSeries()
        {
            /*
                [ 'Firefox', 45.0],
				['IE', 26.8],
				{
				    name: 'Chrome',
				    y: 12.8,
				    sliced: true,
				    selected: true
				},
				['Safari', 8.5],
				['Opera', 6.2],
				['Others', 0.7]
             */
            DataTable dtmain = report3dts;
            StringBuilder sb = new StringBuilder();
            // sb.AppendLine("[");

            int rowNum = 1;
            string str直接 = "";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 += string.Format("['{0}', {1}]", dr["融资期限"], dr["占比"]);
                if (rowindex != report3dts.Rows.Count)
                {
                    str直接 += ",";
                }
                rowindex++;
            }

            //  str直接 += "]}";


            sb.AppendLine(str直接);
            rowNum++;

            //sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 4融资租赁同期台数对比分析
        private DataTable _report4dts = null;
        private DataTable report4dts
        {
            get
            {
                if (_report4dts == null)
                {

                    _report4dts = FXFinanceLeaseController.ReportGetRZZRTQTB();
                }
                return _report4dts;
            }
        }

        private void DataBind4()
        {
            //合计行处理
            DataTable dt = report4dts.Copy();
            DataRow drnew = dt.NewRow();
            //合计

            foreach (DataColumn dc in dt.Columns)
            {
                drnew[dc.ColumnName] = 0;
            }
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "年份")
                    continue;
                foreach (DataRow dr in dt.Rows)
                {
                    drnew[dc.ColumnName] = Convert.ToInt32(drnew[dc.ColumnName]) + (dr[dc.ColumnName] is DBNull ? 0 : Convert.ToInt32(dr[dc.ColumnName]));
                }
            }
            // drnew["占比"] = "100%";
            drnew["年份"] = "合计";
            dt.Rows.Add(drnew);
            //合计列处理
            DataColumn sumdc = new DataColumn("合计", typeof(int));
            dt.Columns.Add(sumdc);
            foreach (DataRow dr in dt.Rows)
            {
                int sum = 0;
                foreach (DataColumn dc in dt.Columns)
                {
                    if (dc.ColumnName == "年份" || dc.ColumnName == "合计")
                        continue;
                    sum += dr[dc.ColumnName] is DBNull?0:Convert.ToInt32(dr[dc.ColumnName]);
                }
                dr["合计"] = sum;
            }

            this.gv4.DataSource = dt;
            this.gv4.DataBind();
        }

        protected string GetReport4JSCategories()
        {
            DataTable dtcl = report4dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int colindex = 1;
            foreach (DataColumn dc in dtcl.Columns)
            {
                if (dc.ColumnName == "年份")
                    continue;
                sb.AppendFormat("'{0}'", dc.ColumnName);
                if (colindex != dtcl.Columns.Count-1)
                {
                    sb.AppendFormat(",");
                }
                colindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport4JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report4dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            string str直接 = "";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接= "{name: '" + dr["年份"].ToString() + "',data:[";
                int cNum = 0;
                foreach (DataColumn dc in dtmain.Columns)
                {
                    if (dc.ColumnName == "年份")
                        continue;
                    if(!(dr[dc.ColumnName] is  DBNull))
                    {
                        str直接 += string.Format("[{0},{1}]", cNum, dr[dc.ColumnName]);
                        if (cNum != dtmain.Columns.Count - 2)
                        {
                            str直接 += ",";
                        }
                    }
                    cNum++;
                }
                //去掉最后一个逗号
                string lastChar = str直接.Substring(str直接.Length - 1, 1);
                if (lastChar == ",")
                    str直接 = str直接.Substring(0, str直接.Length - 1);

                str直接 += "]}";
                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                sb.AppendLine(str直接);
                rowindex ++;
            }
            ///-------------------
            //int cNum = 1;
            //foreach (DataColumn dc in dtmain.Columns)
            //{

            //    if (dc.ColumnName == "月份")
            //        continue;

            //    string str直接 = "{name: '" + dc.ColumnName + "',data:[";
            //    int rowindex = 1;
            //    foreach (DataRow dr in dtmain.Rows)
            //    {
            //        str直接 += Convert.ToDecimal(dr[dc.ColumnName]).ToString("f2");

            //        if (rowindex != dtmain.Rows.Count)
            //        {
            //            str直接 += ",";
            //        }
            //        rowindex++;
            //    }
            //    str直接 += "]}";

            //    if (cNum != dtmain.Columns.Count - 1)
            //    {
            //        str直接 += ",";
            //    }
            //    sb.AppendLine(str直接);

            //    cNum++;
            //}
            //-----------------------
            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion

        #region 5融资租赁同期融资额对比分析
      
        private DataTable _report5dts = null;
        private DataTable report5dts
        {
            get
            {
                if (_report5dts == null)
                {

                    _report5dts = FXFinanceLeaseController.ReportGetRZZRTQRZTB();
                }
                return _report5dts;
            }
        }

        private void DataBind5()
        {

            //合计行处理
            DataTable dt = report5dts.Copy();
            DataRow drnew = dt.NewRow();
            //合计

            foreach (DataColumn dc in dt.Columns)
            {
                drnew[dc.ColumnName] = 0;
            }
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "年份")
                    continue;
                foreach (DataRow dr in dt.Rows)
                {
                    drnew[dc.ColumnName] = Convert.ToInt32(drnew[dc.ColumnName]) +(dr[dc.ColumnName] is DBNull ? 0 : Convert.ToInt32(dr[dc.ColumnName]));
                }
            }
            // drnew["占比"] = "100%";
            drnew["年份"] = "合计";
            dt.Rows.Add(drnew);
            //合计列处理
            DataColumn sumdc = new DataColumn("合计", typeof(int));
            dt.Columns.Add(sumdc);
            foreach (DataRow dr in dt.Rows)
            {
                int sum = 0;
                foreach (DataColumn dc in dt.Columns)
                {
                    if (dc.ColumnName == "年份" || dc.ColumnName == "合计")
                        continue;
                    sum += dr[dc.ColumnName] is DBNull ? 0 : Convert.ToInt32(dr[dc.ColumnName]);
                }
                dr["合计"] = sum;
            }

            this.gv5.DataSource = dt;
            this.gv5.DataBind();
        }

        protected string GetReport5JSCategories()
        {
            DataTable dtcl = report5dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int colindex = 1;
            foreach (DataColumn dc in dtcl.Columns)
            {
                if (dc.ColumnName == "年份")
                    continue;
                sb.AppendFormat("'{0}'", dc.ColumnName);
                if (colindex != dtcl.Columns.Count - 1)
                {
                    sb.AppendFormat(",");
                }
                colindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport5JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report5dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            string str直接 = "";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 = "{name: '" + dr["年份"].ToString() + "',data:[";
                int cNum = 0;
                foreach (DataColumn dc in dtmain.Columns)
                {
                    if (dc.ColumnName == "年份")
                        continue;
                    if (!(dr[dc.ColumnName] is DBNull))
                    {
                        str直接 += string.Format("[{0},{1}]", cNum, dr[dc.ColumnName]);
                        if (cNum != dtmain.Columns.Count - 2)
                        {
                            str直接 += ",";
                        }
                    }
                    cNum++;
                }
                //去掉最后一个逗号
                string lastChar = str直接.Substring(str直接.Length - 1, 1);
                if (lastChar == ",")
                    str直接 = str直接.Substring(0, str直接.Length - 1);

                str直接 += "]}";
                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                sb.AppendLine(str直接);
                rowindex++;
            }
            
            sb.AppendLine("]");
            return sb.ToString();
        }


        protected string getmoneyshow(object v)
        {         
            try
            {
                return Convert.ToDecimal(v).ToString("0,0.00");
            }
            catch
            {
                return "0";
            }
        }
        #endregion 

        #region 6各年度报单台数（按接单日期确认）
        private DataTable _report6dts = null;
        private DataTable report6dts
        {
            get
            {
                if (_report6dts == null)
                {

                    _report6dts = FXFinanceLeaseController.ReportGetGNDBDTS();
                }
                return _report6dts;
            }
        }

        private void DataBind6()
        {
            //合计行处理
            DataTable dt = report6dts.Copy();
            DataRow drnew = dt.NewRow();
            //合计

            foreach (DataColumn dc in dt.Columns)
            {
                drnew[dc.ColumnName] = 0;
            }
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "年份")
                    continue;
                foreach (DataRow dr in dt.Rows)
                {
                    drnew[dc.ColumnName] = Convert.ToInt32(drnew[dc.ColumnName]) + (dr[dc.ColumnName] is DBNull ? 0 : Convert.ToInt32(dr[dc.ColumnName]));
                }
            }
            // drnew["占比"] = "100%";
            drnew["年份"] = "合计";
            dt.Rows.Add(drnew);

            //合计列处理
            DataColumn sumdc = new DataColumn("合计", typeof(int));
            dt.Columns.Add(sumdc);
            foreach (DataRow dr in dt.Rows)
            {
                int sum = 0;
                foreach (DataColumn dc in dt.Columns)
                {
                    if (dc.ColumnName == "年份" || dc.ColumnName == "合计")
                        continue;
                    sum += dr[dc.ColumnName] is DBNull ? 0 : Convert.ToInt32(dr[dc.ColumnName]);
                }
                dr["合计"] = sum;
            }

            this.gv6.DataSource = dt;
            this.gv6.DataBind();
        }

        protected string GetReport6JSCategories()
        {
            DataTable dtcl = report6dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int colindex = 1;
            foreach (DataColumn dc in dtcl.Columns)
            {
                if (dc.ColumnName == "年份")
                    continue;
                sb.AppendFormat("'{0}'", dc.ColumnName);
                if (colindex != dtcl.Columns.Count - 1)
                {
                    sb.AppendFormat(",");
                }
                colindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport6JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report6dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            string str直接 = "";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 = "{name: '" + dr["年份"].ToString() + "',data:[";
                int cNum = 0;
                foreach (DataColumn dc in dtmain.Columns)
                {
                    if (dc.ColumnName == "年份")
                        continue;
                    if (!(dr[dc.ColumnName] is DBNull))
                    {
                        str直接 += string.Format("[{0},{1}]", cNum, dr[dc.ColumnName]);
                        if (cNum != dtmain.Columns.Count - 2)
                        {
                            str直接 += ",";
                        }
                    }
                    cNum++;
                }
                //去掉最后一个逗号
                string lastChar = str直接.Substring(str直接.Length - 1, 1);
                if (lastChar == ",")
                    str直接 = str直接.Substring(0, str直接.Length - 1);

                str直接 += "]}";
                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                sb.AppendLine(str直接);
                rowindex++;
            }

            sb.AppendLine("]");
            return sb.ToString();
        }
        #endregion 
    }
}